package com.example.myaccounting.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myaccounting.R;
import com.example.myaccounting.fragment.fragment_01.Fragme_01;
import com.example.myaccounting.fragment.fragment_02.Fragme_02;
import com.example.myaccounting.fragment.fragment_03.Fragme_03;
import com.example.myaccounting.fragment.fragment_04.Fragme_04;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private LinearLayout ll_jz,ll_jt,ll_fx,ll_wd;
    private ImageView iv_jz,iv_jt,iv_fx,iv_wd;
    private TextView tv_jz,tv_jt,tv_fx,tv_wd;
    private ViewPager viewpager;
    private List<Fragment>fragments= new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setViews();
        setFrame();
        setListeners();
    }
    //只显示一次启动页（ App 没被 kill 的情况下）
    @Override
    public void onBackPressed() {
        //super.onBackPressed();不调用父类的方法
        Intent intent=new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    private void setFrame() {
        fragments.add(new Fragme_01());
        fragments.add(new Fragme_02());
        fragments.add(new Fragme_03());
        fragments.add(new Fragme_04());
        InnerAdapter adapter = new InnerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);

    }

    private void setListeners() {
        ll_jz.setOnClickListener(this);
        ll_jt.setOnClickListener(this);
        ll_fx.setOnClickListener(this);
        ll_wd.setOnClickListener(this);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    efom(0);

                }else if (position==1){
                    efom(1);
                }
                else if (position==2){
                    efom(2);
                }
                else if (position==3){
                    efom(3);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setViews() {
        viewpager=(ViewPager)findViewById(R.id.viewpager);
        ll_jz=(LinearLayout)findViewById(R.id.ll_jz);
        ll_jt=(LinearLayout)findViewById(R.id.ll_jt);
        ll_fx=(LinearLayout)findViewById(R.id.ll_fx);
        ll_wd=(LinearLayout)findViewById(R.id.ll_wd);
        iv_jz=(ImageView)findViewById(R.id.iv_jz);
        iv_jt=(ImageView)findViewById(R.id.iv_jt);
        iv_fx=(ImageView)findViewById(R.id.iv_fx);
        iv_wd=(ImageView)findViewById(R.id.iv_wd);
        tv_jz=(TextView)findViewById(R.id.tv_jz);
        tv_jt=(TextView)findViewById(R.id.tv_jt);
        tv_fx=(TextView)findViewById(R.id.tv_fx);
        tv_wd=(TextView)findViewById(R.id.tv_wd);
        iv_jz.setImageResource(R.drawable.jz_1);
        tv_jz.setTextColor(Color.parseColor("#d81e06"));

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_jz:
                efom(0);
                break;
            case R.id.ll_jt:
                efom(1);
                break;
            case R.id.ll_fx:
                efom(2);
                break;
            case R.id.ll_wd:
                efom(3);
                break;
        }
    }

    private void efom(int i) {
        if(i==0){
            iv_jz.setImageResource(R.drawable.jz_1);
            iv_jt.setImageResource(R.drawable.jt_2);
            iv_fx.setImageResource(R.drawable.fx_2);
            iv_wd.setImageResource(R.drawable.wd_2);
            tv_jz.setTextColor(Color.parseColor("#d81e06"));
            tv_jt.setTextColor(Color.parseColor("#707070"));
            tv_fx.setTextColor(Color.parseColor("#707070"));
            tv_wd.setTextColor(Color.parseColor("#707070"));
            viewpager.setCurrentItem(i);
        }else if(i==1){
            iv_jz.setImageResource(R.drawable.jz_2);
            iv_jt.setImageResource(R.drawable.jt_1);
            iv_fx.setImageResource(R.drawable.fx_2);
            iv_wd.setImageResource(R.drawable.wd_2);
            tv_jz.setTextColor(Color.parseColor("#707070"));
            tv_jt.setTextColor(Color.parseColor("#d81e06"));
            tv_fx.setTextColor(Color.parseColor("#707070"));
            tv_wd.setTextColor(Color.parseColor("#707070"));
            viewpager.setCurrentItem(i);
        }
        else if(i==2){
            iv_jz.setImageResource(R.drawable.jz_2);
            iv_jt.setImageResource(R.drawable.jt_2);
            iv_fx.setImageResource(R.drawable.fx_1);
            iv_wd.setImageResource(R.drawable.wd_2);
            tv_jz.setTextColor(Color.parseColor("#707070"));
            tv_jt.setTextColor(Color.parseColor("#707070"));
            tv_fx.setTextColor(Color.parseColor("#d81e06"));
            tv_wd.setTextColor(Color.parseColor("#707070"));
            viewpager.setCurrentItem(i);
        }else if(i==3){
            iv_jz.setImageResource(R.drawable.jz_2);
            iv_jt.setImageResource(R.drawable.jt_2);
            iv_fx.setImageResource(R.drawable.fx_2);
            iv_wd.setImageResource(R.drawable.wd_1);
            tv_jz.setTextColor(Color.parseColor("#707070"));
            tv_jt.setTextColor(Color.parseColor("#707070"));
            tv_fx.setTextColor(Color.parseColor("#707070"));
            tv_wd.setTextColor(Color.parseColor("#d81e06"));
            viewpager.setCurrentItem(i);

        }
    }
    class InnerAdapter extends FragmentPagerAdapter {
        public InnerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
